package com.thprojects.myphotomemories.view.components;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.thprojects.myphotomemories.R;
import com.thprojects.myphotomemories.controller.FSController;
import com.thprojects.myphotomemories.model.WebPhoto;

public class WebPhotoAdapter extends BaseAdapter {
    
	private Context mContext;
    List<WebPhoto> mPhotos;

    public WebPhotoAdapter(Context c, List<WebPhoto> photos) {
        mContext = c;
        mPhotos = photos;
    }

    public int getCount() {
        return mPhotos.size();
    }

    public Object getItem(int position) {
        return mPhotos.get(position);
    }

    public long getItemId(int position) {
        return mPhotos.get(position).getmWebId();
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        FSController fsc = FSController.getFSController(mContext);
        try {
			imageView.setImageBitmap(fsc.decodeSampledBitmap(mPhotos.get(position).getmUrl(), 85, 85));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return imageView;
    }

}